package nl.hr.minor.mobilelife.j1213.jlr.findit;

import java.util.HashMap;

import nl.hr.minor.mobilelife.j1213.jlr.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class SearchDifferenceThree extends Activity implements View.OnTouchListener, View.OnClickListener{

	public static final String LEVEL = "com.example.minigames.LEVEL_THREE";
	public static final String SCORE = "com.example.minigames.SCORE";
	public static final String MISTAKES = "com.example.minigames.MISTAKES";

	private int progressScore;
	private int counter;
	private int mistakes;
	private int differences;

	private TextView textView;
	private TextView mistakesTextView;

	private ImageView imageOne;
	private ImageView imageTwo;

	private Button buttonOne;

	private ProgressBar progressBar;

	private HashMap<String, Boolean> differenceFound;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.findit_search_difference_three);
		initVars();
	}

	@Override
	protected void onResume() {
		super.onResume();
		new Thread (new Runnable(){
			@Override
			public void run() {
				while(progressScore > 0){
					try {
						Thread.sleep(1000);
						progressScore -= 1;
						progressBar.setProgress(progressScore);

					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

	public void initVars(){
		differenceFound = new HashMap<String, Boolean>();
		differenceFound.put("rood", false);
		differenceFound.put("groen", false);
		differenceFound.put("zwart", false);
		differenceFound.put("geel", false);
		differenceFound.put("bruin", false);
		differenceFound.put("oranje", false);
		differenceFound.put("grijs", false);
		differenceFound.put("paars", false);
		differenceFound.put("blauw", false);
		differenceFound.put("lichtblauw", false);
		differenceFound.put("magenta", false);

		imageOne = (ImageView) findViewById(R.id.imageOne);
		imageOne.setOnTouchListener(this);
		imageTwo = (ImageView) findViewById(R.id.imageTwo);
		imageTwo.setOnTouchListener(this);

		buttonOne = (Button) findViewById(R.id.buttonOne);
		buttonOne.setOnClickListener(this);
		progressBar = (ProgressBar) findViewById(R.id.progressBarOne);
		textView = (TextView) findViewById(R.id.found);
		mistakesTextView = (TextView) findViewById(R.id.mistakes);

		differences = 11;
		progressScore = 100;
		counter = 0;
		mistakes = 0;
	}

	@Override
	public boolean onTouch(View view, MotionEvent event) {

		final int ACTION = event.getAction();

		final int cordX = (int) event.getX();
		final int cordY = (int) event.getY();

		try{
			int touchColor = getTouchedColor (R.id.imageOverlayOne, cordX, cordY);

			if(ACTION == MotionEvent.ACTION_UP){
				if (closeMatch(Color.rgb(255, 1, 0), touchColor, 25)) {
					if(!differenceFound.get("rood")){
						differenceFound.put("rood", true);
						updateSearchCounter();
					}
					return true;
				}else if(closeMatch(Color.rgb(0,128,1), touchColor, 25)){
					if(!differenceFound.get("groen")){
						differenceFound.put("groen", true);
						updateSearchCounter();
					}
					return true;
				}else if(closeMatch(Color.rgb(0, 0, 0), touchColor, 25)){
					if(!differenceFound.get("zwart")){
						differenceFound.put("zwart", true);
						updateSearchCounter();
					}
					return true;
				}else if (closeMatch(Color.rgb(255,255,1), touchColor, 25)) {
					if(!differenceFound.get("geel")){
						differenceFound.put("geel", true);
						updateSearchCounter();
					}
					return true;
				}else if(closeMatch(Color.rgb(150, 75, 0), touchColor, 25)){
					if(!differenceFound.get("bruin")){
						differenceFound.put("bruin", true);
						updateSearchCounter();
					}
					return true;
				}else if(closeMatch(Color.rgb(255, 127, 0), touchColor, 25)){
					if(!differenceFound.get("oranje")){
						differenceFound.put("oranje", true);
						updateSearchCounter();
					}
					return true;
				}else if(closeMatch(Color.rgb(128, 128, 128), touchColor, 25)){
					if(!differenceFound.get("grijs")){
						differenceFound.put("grijs", true);
						updateSearchCounter();
					}
					return true;
				}else if(closeMatch(Color.rgb(129, 0, 127), touchColor, 25)){
					if(!differenceFound.get("paars")){
						differenceFound.put("paars", true);
						updateSearchCounter();
					}
					return true;
				}else if(closeMatch(Color.rgb(0, 0, 254), touchColor, 25)){
					if(!differenceFound.get("blauw")){
						differenceFound.put("blauw", true);
						updateSearchCounter();
					}
					return true;
				}else if(closeMatch(Color.rgb(0, 255, 255), touchColor, 25)){
					if(!differenceFound.get("lichtblauw")){
						differenceFound.put("lichtblauw", true);
						updateSearchCounter();
					}
					return true;
				}else if(closeMatch(Color.rgb(255, 0, 255), touchColor, 25)){
					if(!differenceFound.get("magenta")){
						differenceFound.put("magenta", true);
						updateSearchCounter();
					}
					return true;
				}else{
					mistakesTextView.setText("foutjes: "+(++mistakes));
				}
			}
		}catch(IllegalArgumentException e){
			System.err.print("x of y buiten grens.");
		}
		return true;
	}

	public void updateSearchCounter(){
		textView.setText("gevonden: " + (++counter));
		if(counter == differences){
			savePreferences();
			startNextLevel();
		}
	}

	private void savePreferences(){
		SharedPreferences scoreSharedPreferences = 
				getSharedPreferences(LEVEL, Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = scoreSharedPreferences.edit();
		editor.putInt(SCORE, progressScore);
		editor.putInt(MISTAKES, mistakes);
		editor.commit();
	}

	private void startNextLevel(){
		Intent intent = new Intent(this, Scores.class);
		startActivity(intent);
		finish();
	}


	public int getTouchedColor(int imageOverlayId, int x, int y){
		ImageView imageView = (ImageView) findViewById(imageOverlayId);
		imageView.setDrawingCacheEnabled(true);
		Bitmap imageOverlay = Bitmap.createBitmap(imageView.getDrawingCache());
		imageView.setDrawingCacheEnabled(false);
		return imageOverlay.getPixel(x, y);
	}

	public boolean closeMatch (int color1, int color2, int tolerance) {
		if ((int) Math.abs (Color.red (color1) - Color.red (color2)) > tolerance ) 
			return false;
		if ((int) Math.abs (Color.green (color1) - Color.green (color2)) > tolerance ) 
			return false;
		if ((int) Math.abs (Color.blue (color1) - Color.blue (color2)) > tolerance ) 
			return false;
		return true;
	}

	@Override
	public void onClick(View v) {
		mistakesTextView.setText("foutjes: "+ (++mistakes));
	}
}